<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <!--下面这个用于显示模态框-->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
    <script th:src="@{/static/main.js}"></script>



</head>
<body>
<!--颜醉-->
<!--用户地址管理页面-->



<div class="container-fluid" id="app" style="margin-top: 10px;margin-bottom: 100px">

    <div class="container" style="margin-top: 10px">

        <div class="row">
            <div class="col-md-12">
                <p style="background-color: cornflowerblue ; color: white; font-size: 30px; padding-left: 10px; border-radius: 4px">
                    <a th:href="@{/usercenter.html}" style="font-size: 30px;color: white;text-decoration: none">个人中心</a>
                </p>

            </div>
        </div>

        <div class="row" >
            <div class="col-md-8 span8" v-if="addressList == ''">
                <h3 class="text-info">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;还没有地址~快去添加吧</h3>
            </div>

            <div class="col-md-8 span8" v-if="addressList != ''">
                <ul class="nav nav-tabs nav-stacked" v-for="a in addressList">
                    <li v-if="a.status == 0">
                        <a :href="'/address/toSettingAddress?addressId=' + a.id" style="font-size: 20px">{{a.isDefault==1?'默认&nbsp;&nbsp;&nbsp;':'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'}}{{a.province}}{{a.city}}{{a.area}}{{a.detailAddress}}  {{a.tel}}  {{a.receiver}} </a>
                    </li>

                </ul>
            </div>
            <div class="col-md-4">
                <button class="btn btn-primary" data-toggle="modal" data-target="#myModal" style="margin-top: 10px" @click="clearAddForm">新增地址</button>
            </div>
        </div>


        <!-- Modal -->
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">新增地址</h4>
                    </div>
                    <div class="modal-body">
                        <form>
                            <div class="form-group">
                                <label for="provinceId">省</label>
                                <select class="form-control" id="provinceId" placeholder="请选择省份" v-model="provinceId" @change="refreshCity">
                                    <option value="-1">请选择省份</option>
                                    <option v-for="province in provinceList" :value="province.id" v-text="province.name"></option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="cityId">市</label>
                                <select class="form-control" id="cityId" placeholder="请选择城市" v-model="cityId" @change="refreshCounty">
                                    <option value="-1">请选择城市</option>
                                    <option v-for="city in cityList" :value="city.id" v-text="city.name"></option>
                                </select>
                            </div>

                            <div class="form-group">
                                <label for="areaId">区</label>
                                <select class="form-control" id="areaId" placeholder="请选择区域"  v-model="countyId">
                                    <option value="-1">请选择区域</option>
                                    <option v-for="county in countyList" :value="county.id" v-text="county.name"></option>
                                </select>
                            </div>

                            <div class="form-group">
                                <label for="detailAddress">详细地址</label>
                                <input type="text" v-model="detailAddress" class="form-control" id="detailAddress" placeholder="请输入详细地址">
                            </div>

                            <div class="form-group">
                                <label for="tel">手机号</label>
                                <input type="text" v-model="tel" class="form-control" id="tel" placeholder="请输入手机号">
                            </div>

                            <div class="form-group">
                                <label for="receiver">收件人姓名</label>
                                <input type="text" v-model="receiver" class="form-control" id="receiver" placeholder="请输入收件人">
                            </div>
                            <div class="form-group">
                                <label for="code">邮编</label>
                                <input type="text" v-model="emailCode" class="form-control" id="code" placeholder="请输入邮编">
                            </div>
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" v-model="isDefault" id="isDefault" > 是否默认
                                </label>
                            </div>
                        </form>

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary" @click.prevent="saveAddress">保存</button>
                    </div>
                </div>
            </div>
        </div>

    </div>


</div>

<script>

    console.log(".........useraddress...........")

    let useraddress = new Vue({
        el: "#app"
        ,
        data: {
            addressList: [],

            provinceList:[],
            provinceId:-1,
            cityList:[],
            cityId:-1,
            countyList:[],
            countyId:-1,
            detailAddress:'',
            tel:'',
            receiver:'',
            emailCode:'',
            isDefault:0,

        }
        ,
        methods: {
            initAddressList() {
                let _this = this;
                $.get("/address/all",function (data) {
                    _this.addressList = data;

                })
            }
            ,
            initProvinceList(){  //省 / 直辖市
                let _this = this;

                $.get("/china/queryList?pid=0",function (data) {
                    console.log(data)

                    _this.provinceList = data;

                    //市 区 选择项要置空
                    _this.cityList=[];
                    _this.cityId = -1;
                    _this.countyList = [];
                    _this.countyId = -1;

                })


            }
            ,
            refreshCity(){   //市  / 直辖市的区
                console.log(this.provinceId)

                let _this = this;

                $.get("/china/queryList?pid="+this.provinceId,function (data) {
                    console.log(data)

                    _this.cityList = data;

                    //把县数据置空
                    _this.countyList = [];
                    _this.countyId = -1;
                    _this.cityId = -1;
                })

            }
            ,
            refreshCounty(){   // 县 / -
                console.log(this.cityId)

                let _this = this;

                $.get("/china/queryList?pid="+this.cityId,function (data) {
                    console.log(data)

                    _this.countyList = data;
                })

            }
            ,
            saveAddress(){

                //搜集表单数据

                //省市区要id转name
                let provinceName = "";
                for(let item of this.provinceList){
                    if (item.id == this.provinceId){
                        provinceName = item.name;
                    }
                }

                let cityName = "";
                for(let item of this.cityList){
                    if (item.id == this.cityId){
                        cityName = item.name;
                    }
                }

                let countyName = "";
                for(let item of this.countyList){
                    if (item.id == this.countyId){
                        countyName = item.name;
                    }
                }

                let _isDefault = this.isDefault;
                if (_isDefault) {
                    _isDefault = 1;
                } else {
                    _isDefault = 0;
                }

                if (!this.isTel(this.tel)) {
                    alert("电话格式错误");
                    return false;
                }
                if (!this.isMailCode(this.emailCode)) {
                    alert("邮编格式错误");
                    return false;
                }

                let params = {
                    province:provinceName,
                    city:cityName,
                    area:countyName,
                    detailAddress:this.detailAddress,
                    tel:this.tel,
                    receiver:this.receiver,
                    emailCode:this.emailCode,
                    isDefault:_isDefault
                }

                console.log(params)

                let _this=this;

                //数据提交后端接口,新增地址
                $.post("/address/add",params,function (data) {
                    console.log(data)

                    //关闭弹窗
                    $("#myModal").modal('hide')

                    //刷新可选地址
                    _this.initAddressList();
                    _this.cityList = [];
                    _this.countyList = [];
                })


            }
            ,
            clearAddForm(){
                //打开新增地址表单时,清空表单数据
                this.provinceId=-1;
                this.cityId=-1;
                this.countyId=-1;
                this.detailAddress="";
                this.tel="";
                this.receiver="";
                this.emailCode="";
                this.isDefault=0;
            }
            ,
            isTel(n) {
                var reg=/^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
                return reg.test(n);
            }
            ,
            isMailCode(n) {
                var reg=/[1-9]\d{5}(?!\d)/;
                return reg.test(n);
            }
        }
        ,
        created() {
            this.initAddressList();
            this.initProvinceList();
        }

    });


</script>


</body>
</html>

